#include <iostream>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int INF=0x3f3f3f3f;
const int N=25100;
int t,r,p,s;
int id[N];
int rd[N];
int dist[N];
int st[N];
vector<int> block[N];
vector<pair<int,int>> edge1[N];

queue<int> q;
struct cmp{
	bool operator()(pair<int,int>& p1,pair<int,int>& p2)
	{
		return p1.second>p2.second;
	}
};
void dfs(int x,int pos)
{
	id[x]=pos;
	block[pos].push_back(x);
	for(auto& e:edge1[x])
	{
		int aim=e.first;
		if(!id[aim])
		{
			dfs(aim,pos);
		}
	}
}
void dijkstra(int x)
{
	priority_queue<pair<int,int>,vector<pair<int,int>>,cmp> pq;
	
	for(int& e:block[x])
	{
		pq.push({e,dist[e]});
	}
	
	while(pq.size())
	{
		int tmp=pq.top().first;
		
		pq.pop();
		if(st[tmp])
			continue;
		st[tmp]=1;
		for(auto& e:edge1[tmp])
		{
			if(id[tmp]!=id[e.first] && --rd[id[e.first]]==0)
				q.push(id[e.first]);
			if(dist[e.first]>dist[tmp]+e.second)
			{
				dist[e.first]=dist[tmp]+e.second;
				if(id[tmp]==id[e.first])
					pq.push({e.first,dist[e.first]});
			}
		}
	}
}
void topsort(int pos)
{
	memset(dist,INF,sizeof(dist));
	dist[s]=0;
	for(int i=1;i<=pos;i++)
	{
		if(rd[i]==0)
			q.push(i);
	}
	while(q.size())
	{
		int tmp=q.front();
		q.pop();
		dijkstra(tmp);
	}
}
int main()
{
	cin>>t>>r>>p>>s;
	for(int i=0;i<r;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		edge1[a].push_back({b,c});
		edge1[b].push_back({a,c});
	}

	memset(id,0,sizeof(id));
	int pos=0;
	for(int i=1;i<=t;i++)
	{
		if(!id[i])
		{
			dfs(i,++pos);
		}
	}
	for(int i=0;i<p;i++)
	{
		int a,b,c;
		cin>>a>>b>>c;
		edge1[a].push_back({b,c});
		rd[id[b]]++;
	}
	topsort(pos);
	for(int i=1;i<=t;i++)
	{
		if(dist[i]>INF/2)
			cout<<"NO PATH"<<endl;
		else{
			cout<<dist[i]<<endl;
		}
	}
	return 0;
}
